#include "StdAfx.h"
#include "RecentActionsManager.h"

RecentActionsManager::RecentActionsManager():
m_CopyCount(0),
m_MoveCount(0)
{
	ReadAll();
}

RecentActionsManager::~RecentActionsManager()
{
	SaveAll();
}

void RecentActionsManager::CalculateCounts()
{
	m_CopyCount = 0;

	for (int i=0; i<5; i++)
	{
		if (m_LastCopyPaths[i] == L"")
		{
			break;
		}
		else
		{
			m_CopyCount++;
		}
	}

	m_MoveCount = 0;

	for (int i=0; i<5; i++)
	{
		if (m_LastMovePaths[i] == L"")
		{
			break;
		}
		else
		{
			m_MoveCount++;
		}
	}

}

void RecentActionsManager::SaveAll()
{
	WriteStringValueToRegistry(L"RecentCopyPath00", m_LastCopyPaths[0]); 
	WriteStringValueToRegistry(L"RecentCopyPath01", m_LastCopyPaths[1]); 
	WriteStringValueToRegistry(L"RecentCopyPath02", m_LastCopyPaths[2]); 
	WriteStringValueToRegistry(L"RecentCopyPath03", m_LastCopyPaths[3]); 
	WriteStringValueToRegistry(L"RecentCopyPath04", m_LastCopyPaths[4]);

	WriteStringValueToRegistry(L"RecentMovePath00", m_LastMovePaths[0]); 
	WriteStringValueToRegistry(L"RecentMovePath01", m_LastMovePaths[1]); 
	WriteStringValueToRegistry(L"RecentMovePath02", m_LastMovePaths[2]);
	WriteStringValueToRegistry(L"RecentMovePath03", m_LastMovePaths[3]); 
	WriteStringValueToRegistry(L"RecentMovePath04", m_LastMovePaths[4]);

	CalculateCounts();
}

void RecentActionsManager::ReadAll()
{
	m_LastCopyPaths[0] = ReadStringValueFromRegistry(L"RecentCopyPath00");
	m_LastCopyPaths[1] = ReadStringValueFromRegistry(L"RecentCopyPath01");
	m_LastCopyPaths[2] = ReadStringValueFromRegistry(L"RecentCopyPath02");
	m_LastCopyPaths[3] = ReadStringValueFromRegistry(L"RecentCopyPath03");
	m_LastCopyPaths[4] = ReadStringValueFromRegistry(L"RecentCopyPath04");

	m_LastMovePaths[0] = ReadStringValueFromRegistry(L"RecentMovePath00");
	m_LastMovePaths[1] = ReadStringValueFromRegistry(L"RecentMovePath01");
	m_LastMovePaths[2] = ReadStringValueFromRegistry(L"RecentMovePath02");
	m_LastMovePaths[3] = ReadStringValueFromRegistry(L"RecentMovePath03");
	m_LastMovePaths[4] = ReadStringValueFromRegistry(L"RecentMovePath04");

	CalculateCounts();
}

void RecentActionsManager::AddCopyPath(const wstring& path)
{
	int found = -1;

	for (int i=0; i<5; i++)
	{
		if (m_LastCopyPaths[i] == path)
		{
			found = i;
			break;
		}
	}

	if (found == -1)
	{
		for (int i=4; i>0; i--)
		{
			m_LastCopyPaths[i] = m_LastCopyPaths[i-1];
		}

		m_LastCopyPaths[0] = path;

		m_CopyCount++;

		if (m_CopyCount > 5)
		{
			m_CopyCount = 5;
		}
	}
	else if (found > 0 && found < 5)
	{
		// Do swap
		wstring tmp = L"";
		tmp = m_LastCopyPaths[found];

		for (int i=found; i>0; i--)
		{
			m_LastCopyPaths[i] = m_LastCopyPaths[i-1];
		}

		m_LastCopyPaths[0] = tmp;
	}

}

void RecentActionsManager::AddMovePath(const wstring& path)
{
	int found = -1;

	for (int i=0; i<5; i++)
	{
		if (m_LastMovePaths[i] == path)
		{
			found = i;
			break;
		}
	}

	if (found == -1)
	{
		for (int i=4; i>0; i--)
		{
			m_LastMovePaths[i] = m_LastMovePaths[i-1];
		}

		m_LastMovePaths[0] = path;

		m_MoveCount++;

		if (m_MoveCount > 5)
		{
			m_MoveCount = 5;
		}
	}
	else if (found > 0 && found < 5)
	{
		// Do swap
		wstring tmp = L"";
		tmp = m_LastMovePaths[found];

		for (int i=found; i>0; i--)
		{
			m_LastMovePaths[i] = m_LastMovePaths[i-1];
		}

		m_LastMovePaths[0] = tmp;
	}
}

wstring RecentActionsManager::ReadStringValueFromRegistry(const wstring& valueName)
{
	wstring retVal = L"";

	HKEY hkey;
	DWORD dwType, dwSize;

	long lResult = RegOpenKeyEx(
		HKEY_CURRENT_USER,
		TEXT("Software\\FilerFrog"),
		0,
		KEY_QUERY_VALUE,						
		&hkey);

	wchar_t szValue[1024];
	ZeroMemory(&szValue, sizeof(szValue));

	if (lResult == ERROR_SUCCESS)
	{		
		dwType = REG_SZ;					
		dwSize = sizeof(szValue);
		RegQueryValueEx(hkey, valueName.c_str() , 0, &dwType, (LPBYTE)&szValue, &dwSize);
		RegCloseKey(hkey);

		retVal = szValue;
	}

	return retVal;
}

void RecentActionsManager::WriteStringValueToRegistry(const wstring& valueName, const wstring& value)
{
	HKEY hkey;
	DWORD dwDisposition; 
	DWORD dwType, dwSize;

	long lResult = RegCreateKeyEx(
		HKEY_CURRENT_USER,
		TEXT("Software\\FilerFrog"),
		0,
		NULL,
		REG_OPTION_NON_VOLATILE,
		KEY_ALL_ACCESS,
		NULL,
		&hkey,
		&dwDisposition);

	wchar_t szValue[1024];
	ZeroMemory(&szValue, sizeof(szValue));

	wcscpy_s(szValue, 1024, value.c_str());

	if (lResult == ERROR_SUCCESS)
	{		
		dwType = REG_SZ;		
		dwSize = (DWORD)(lstrlen(szValue)+1) * sizeof(wchar_t);
		lResult = RegSetValueEx(hkey, valueName.c_str(), 0, dwType, (PBYTE)&szValue, dwSize);
		RegCloseKey(hkey);
	}
}

void RecentActionsManager::ClearAndSave()
{
	for (int i=0; i<5; i++)
	{
		m_LastCopyPaths[i] = L"";
		m_LastMovePaths[i] = L"";
	}

	SaveAll();
}